﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PurchaseOrders;

public partial class Documents_PurchaseOrder_PurchaseOrderItems : System.Web.UI.Page
{
    public List<PurchaseOrders.Cover> Covers { get; set; }
    public List<PurchaseOrders.Leg> Legs { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        Covers = new List<PurchaseOrders.Cover>();
        Legs = new List<PurchaseOrders.Leg>();
        if (!Page.IsPostBack)
        {

            string orderIdQuery = Request.QueryString["OrderId"];
            int orderId;
            if (int.TryParse(orderIdQuery, out orderId))
            {
                Orders.OrdersDataClassesDataContext dc = new Orders.OrdersDataClassesDataContext();
                var order = from o in dc.PurchaseOrders
                            where o.PurchaseOrderID == orderId
                            select o;
                if (order.Count() > 0)
                {
                    litOrderNo.Text = order.First().OrderNo;
                }
                ViewState.Add("OrderId", orderId);
            }
        }

    }
    protected void btnNewLegItem_Click(object sender, EventArgs e)
    {
        dvProductCoverInfo.Visible = false;
        dvProductLegInfo.Visible = true;
        dvProductLegInfo.ChangeMode(DetailsViewMode.Insert);
        PurchaseOrderItemsDataContext dc = new PurchaseOrderItemsDataContext();
        Legs = dc.Legs.ToList();
    }


    protected void btnNewCoverItem_Click(object sender, EventArgs e)
    {
        dvProductCoverInfo.Visible = true;
        dvProductLegInfo.Visible = false;
        dvProductCoverInfo.ChangeMode(DetailsViewMode.Insert);
        PurchaseOrderItemsDataContext dc = new PurchaseOrderItemsDataContext();
        Covers = dc.Covers.ToList();
    }
    protected void dvProductCoverInfo_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        if (ViewState["OrderId"] != null)
        {
            e.Values.Remove("PurchaseOrderID");
            e.Values.Add("PurchaseOrderID", ViewState["OrderId"]);
        }
    }


    protected void dvProductCoverInfo_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        PurchaseOrderItemsDataContext dc = new PurchaseOrderItemsDataContext();
        if (e.NewMode == DetailsViewMode.Edit ||
                    e.NewMode == DetailsViewMode.Insert)
        {
            Covers = dc.Covers.ToList();
        }
        else
        {
            gvCoverList.DataBind();
        }
    }
    protected void ldsProductInfo_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        foreach (KeyValuePair<string, object> kvp in e.WhereParameters)
        {
            if (kvp.Value == null) { e.Cancel = true; return; }
        }
    }

    protected void dvProductLegInfo_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        if (ViewState["OrderId"] != null)
        {
            e.Values.Remove("PurchaseOrderID");
            e.Values.Add("PurchaseOrderID", ViewState["OrderId"]);
        }
    }


    protected void dvProductLegInfo_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        PurchaseOrderItemsDataContext dc = new PurchaseOrderItemsDataContext();
        if (e.NewMode == DetailsViewMode.Edit ||
                    e.NewMode == DetailsViewMode.Insert)
        {
            Legs = dc.Legs.ToList();
        }
        else
        {
            gvLegList.DataBind();
        }
    }
    protected void ldsProductInfo2_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        foreach (KeyValuePair<string, object> kvp in e.WhereParameters)
        {
            if (kvp.Value == null) { e.Cancel = true; return; }
        }
    }
    protected void gvLegList_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvCoverList.SelectedIndex = -1;
        dvProductLegInfo.Visible = true;
        dvProductCoverInfo.Visible = false;
    }
    protected void gvCoverList_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvLegList.SelectedIndex = -1;
        dvProductLegInfo.Visible = false;
        dvProductCoverInfo.Visible = true;
    }
}
